package com.zylix.simon.drawings.notes;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class DrawingSurface extends SurfaceView implements SurfaceHolder.Callback {
	private static final String TAG = "DrawingSurface";
	
    private Boolean _run;
    protected DrawThread thread;
    private Bitmap mBitmap;
    private SurfaceHolder surfaceholder;
    
    private DrawCmdStack drawcommandstack;
    
    public DrawingSurface(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.i(TAG, "Constructor - DrawingSurface");

        //getHolder().addCallback(this);
        surfaceholder = getHolder();
        surfaceholder.addCallback(this);
        
        drawcommandstack = new DrawCmdStack();
        thread = new DrawThread(getHolder());
    }

    public boolean isDrawingSurfaceClean(){
    	return (drawcommandstack.currentStackLength() == 0); 
    }

    class DrawThread extends  Thread{
        private SurfaceHolder mSurfaceHolder;


        public DrawThread(SurfaceHolder surfaceHolder){
            mSurfaceHolder = surfaceHolder;

        }

        public void setRunning(boolean run) {
            _run = run;
        }


        @Override
        public void run() {
            Canvas canvas = null;
            while (_run){
                try{
                    canvas = mSurfaceHolder.lockCanvas(null);
                    if(mBitmap == null){
                        mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);;
                    }
                    final Canvas c = new Canvas (mBitmap);

                    c.drawColor(0, PorterDuff.Mode.CLEAR);

                    drawcommandstack.executeAll(c);

                    canvas.drawBitmap (mBitmap, 0,  0,null);
                    canvas.drawColor(0, PorterDuff.Mode.CLEAR);

                    drawcommandstack.executeAll(canvas);
                } finally {
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }
            }

        }
    }


    public void addDrawingPath (DrawingPath drawingPath){
        drawcommandstack.addCommand(drawingPath);
    }

    public boolean hasMoreRedo(){
        return drawcommandstack.hasMoreRedo();
    }

    public void redo(){
        drawcommandstack.redo();
    }

    public void undo(){
    	Log.i(TAG, "undo()");
        drawcommandstack.undo();
    }

    public boolean hasMoreUndo(){
        return drawcommandstack.hasMoreRedo();
    }

    public Bitmap getBitmap(){
        return mBitmap;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,  int height) {
        // TODO Auto-generated method stub
        mBitmap =  Bitmap.createBitmap (width, height, Bitmap.Config.ARGB_8888);;
    }


    public void surfaceCreated(SurfaceHolder holder) {
    	Log.i(TAG, "surfaceCreated");
        // TODO Auto-generated method stub
        thread.setRunning(true);
        thread.start();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        boolean retry = true;
        thread.setRunning(false);
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }
    
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		try{
            canvas = surfaceholder.lockCanvas(null);
            if(mBitmap == null){
                mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);;
            }
            final Canvas c = new Canvas (mBitmap);

            c.drawColor(0, PorterDuff.Mode.CLEAR);

            drawcommandstack.executeAll(c);

            canvas.drawBitmap (mBitmap, 0,  0,null);
            canvas.drawColor(0, PorterDuff.Mode.CLEAR);

            drawcommandstack.executeAll(canvas);
        } finally {
        	surfaceholder.unlockCanvasAndPost(canvas);
        }
	}
}
